package gewu.suleju.controller;
import gewu.suleju.model.pojo.BrokerInfo;
import gewu.suleju.model.pojo.HouseInfor;
import gewu.suleju.model.vo.Result;
import gewu.suleju.sevice.imp.BrokerrServiceImp;
import gewu.suleju.sevice.imp.HouseInforServiceImp;
import gewu.suleju.util.JWTUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("BrokerrInfo")
public class BrokerrInfoController {
    @Autowired
    private BrokerrServiceImp brokerrServiceImp;
    @Autowired
    private HouseInforServiceImp houseInforServiceImp;
    @ResponseBody
    @GetMapping("BrokerrInfoByUserId")
    public Result BrokerrInfoByUserId(HttpServletRequest request, HttpServletResponse response){
        String token = request.getHeader("token");
        Claims claims = JWTUtil.checkJWT(token);
        Integer userid=(Integer)claims.get("id");
        List<BrokerInfo> brokerInfoList=new ArrayList<>();
        List<HouseInfor>  houseInforList=houseInforServiceImp.findHouseInforsByUserId(userid);
        for(int i=0;i<houseInforList.size();i++){
            brokerInfoList.add(brokerrServiceImp.findBrokerInfoById(houseInforList.get(i).getBrokerInfoId()));
        }
        return new Result("200","成功",brokerInfoList);
    }
}
